package com.example.light.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.light.system.common.Result;
import com.example.light.system.entity.loginLog;
import com.example.light.system.mapper.loginLogMapper;
import com.example.light.system.service.impl.ILoginLogService;
import com.example.light.system.vo.insertLoginLog;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class loginLogService extends ServiceImpl<loginLogMapper, loginLog> implements ILoginLogService {
    @Resource
    loginLogMapper loginLogMapper;

    /**
     * 测试用例：通过id查找登录日志文件
     *
     * @param id
     * @return
     */
    @Override
    public Result<loginLog> searchLoginFile(int id) {
        loginLog loginLog = loginLogMapper.selectById(id);
        return Result.success("登录日志查找成功",loginLog);
    }

    /**
     * 新增登录日志
     * @param insertLoginLog
     * @return
     */
    @Override
    public Result<loginLog> insertLoginLog(insertLoginLog insertLoginLog) throws UnknownHostException {
        // 创建loginLog的实例对象loginLog
        loginLog loginLog = new loginLog();
        // 获取服务器ip地址
        String hostIp = InetAddress.getLocalHost().getHostAddress().toString();
        // 判断ip地址类型
        try {
            // 服务器ip地址
            InetAddress hostAddress = InetAddress.getByName(hostIp);
            // 请求ip地址
            InetAddress requestAddress = InetAddress.getByName(insertLoginLog.getLoginIp());

            byte[] hostNetId = getNetworkID(hostAddress.getAddress());
            byte[] requestNetId = getNetworkID(requestAddress.getAddress());

            if (hostNetId[0] == requestNetId[0] && hostNetId[1] == requestNetId[1]) {
                loginLog.setIpType("内网访问");
            } else {
                loginLog.setIpType("外网访问");
            }
        } catch (UnknownHostException e) {
            System.out.println("无法解析IP地址");
            e.printStackTrace();
        }
        // 将登录账号信息传入loginLog
        loginLog.setLoginAccount(insertLoginLog.getLoginAccount());
        loginLog.setLoginName("小李");
        loginLog.setLoginRole("admin");
        // 获取登录IP地址并存入loginLog
        loginLog.setLoginIp(insertLoginLog.getLoginIp());
        // 将登录日志信息添加到数据库
        int insert = loginLogMapper.insert(loginLog);
        // 判断登录信息是否录入成功
        if (insert == 1){
            // 获取登录时间
            QueryWrapper<loginLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("login_account", insertLoginLog.getLoginAccount());
            loginLog loginInfo = loginLogMapper.selectOne(queryWrapper);
            LocalDateTime loginTime = loginInfo.getLoginTime();
            loginInfo.setLoginTime(loginTime);
            // 成功返回登录时间，失败则返回0
            return Result.success(loginInfo);
        }else {
            return Result.fail("登录失败");
        }
    }
    /**
     * ip类型判断
     * @param address
     * @return
     */
    private static byte[] getNetworkID(byte[] address) {
        byte[] netId = new byte[2];
        netId[0] = address[0];
        netId[1] = address[1];
        return netId;
    }

    /**
     *获取客户端ip地址
     * @param request
     * @return
     */
    @PostMapping("/getClientIpAddress")
    public String getClientIpAddress(HttpServletRequest request) {
        String clientIp = request.getRemoteAddr(); // 获取客户端IP地址
        return clientIp;
    }
}
